package requeteur.ejb.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.sql.DataSource;

import requeteur.ejb.commun.dto.InformationConnection;
import requeteur.ejb.commun.dto.ResultatRequete;
import requeteur.ejb.commun.dto.ResultatRequeteScript;
import requeteur.ejb.commun.dto.ResultatRequeteSelect;
import requeteur.ejb.commun.dto.ResultatRequeteUpdate;
import requeteur.ejb.commun.exception.ConnectionException;
import requeteur.ejb.commun.outil.ConnectionUtils;
import requeteur.ejb.commun.outil.RequeteurUtils;

/**
 * @author Laurent
 * @version 0.1
 */
@Stateless
public class DaoLancerRequete {


	/**
	 * Constructeur de la classe <code>DaoLancerRequete</code>.
	 */
	public DaoLancerRequete() {
	}

	public ResultatRequeteSelect lancerRequeteSelect(String pRequete, InformationConnection informationConnection) throws ConnectionException {
		final long vDebut = System.currentTimeMillis();
		Connection vConnection = null;
		ResultatRequeteSelect vResultatRequete = null;
		try {
			vConnection = RequeteurUtils.getConnection(informationConnection);
			Statement vStatement = vConnection.createStatement();
			ResultSet vResultat = vStatement.executeQuery(pRequete);
			vResultatRequete = new ResultatRequeteSelect(vResultat);
			vResultatRequete.setDuree(System.currentTimeMillis() - vDebut);
		} catch (SQLException e) {
			throw new ConnectionException("Erreur lors de l'execution de la requete", e);
		} finally {
			ConnectionUtils.fermerConnexion(vConnection);
		}
		return vResultatRequete;
	}

	public ResultatRequeteUpdate lancerRequeteUpdate(String pRequete, InformationConnection informationConnection) throws ConnectionException {
		final long vDebut = System.currentTimeMillis();
		Connection vConnection = null;
		ResultatRequeteUpdate vResultatRequete = null;
		try {
			vResultatRequete = new ResultatRequeteUpdate(pRequete);
			vConnection = RequeteurUtils.getConnection(informationConnection);
			Statement vStatement = vConnection.createStatement();
			vResultatRequete.setNombreOccurence(vStatement.executeUpdate(pRequete));
			vResultatRequete.setDuree(System.currentTimeMillis() - vDebut);
		} catch (SQLException e) {
			throw new ConnectionException("Erreur lors de l'execution de la requete", e);
		} finally {
			ConnectionUtils.fermerConnexion(vConnection);
		}
		return vResultatRequete;
	}

	/**
	 * @param pListeRequete
	 * @return
	 * @throws ConnectionException
	 */
	public ResultatRequeteScript lancerScript(List<String> pListeRequete, InformationConnection informationConnection) throws ConnectionException {
		Connection vConnection = null;
		ResultatRequeteScript vListeResultatRequete = new ResultatRequeteScript();
		try {
			vConnection = RequeteurUtils.getConnection(informationConnection);
			Statement vStatement = vConnection.createStatement();
			for (String vRequete : pListeRequete) {
				final long vDebut = System.currentTimeMillis();
				ResultatRequeteUpdate vResultatRequete = new ResultatRequeteUpdate(vRequete);
				vResultatRequete.setNombreOccurence(vStatement.executeUpdate(vRequete));
				vResultatRequete.setDuree(System.currentTimeMillis() - vDebut);
				vListeResultatRequete.getListeRequete().add(vResultatRequete);
			}
		} catch (SQLException e) {
			throw new ConnectionException("Erreur lors de l'execution du script", e);
		} finally {
			ConnectionUtils.fermerConnexion(vConnection);
		}
		return vListeResultatRequete;
	}
}
